<?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');

class RootCategory extends AppModel {
	public $hasMany = array(
        'Category' => array(
            'className' => 'Category',
            'foreignKey' => 'root_category_id',
             'conditions' => array('Category.deleted' => '0')
			 )
        );
	/**
	 * find all data
	 */
	public function findAllData(){
		return $this->find('all', array(
			'conditions' => array('deleted' => 0)));
	}
	
	/**
	* Get category_name by id
	* @author VuLee
	* @since 2014-11-14
	* @return category_name
	*/
	public function findById($id){
		return $this->find('first',array(
			'conditions' => array('id' => $id, 'deleted' => 0),
			'fields' => array('category_name')
			));
	}
	
	public function findNameAndId(){
		return $this->query("SELECT id, category_name from root_categories where deleted = 0");
	}
	
	public function deleteCategoryByRootId($rootCatId, $userID){
		$sql = "UPDATE categories SET deleted = 1 , ";
		$sql = $sql ."updated_at = NOW(), ";
		$sql = $sql ."updated_by = " . $userID ." ";
		$sql = $sql ." WHERE root_category_id = ". $rootCatId;
		
		return $this->query($sql);
	}
	
}
